home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ftp / RCS / ruserpass.c,v < prev   
Encoding:
Text File  |  1990-10-27  |  8.5 KB  |  413 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     90.10.27.13.48.46;  author shirriff;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     88.07.21.09.57.12;  author ouster;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     88.07.21.09.54.25;  author ouster;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.3
  30. log
  31. @New revision.
  32. @
  33. text
  34. @/*
  35.  * Copyright (c) 1985 Regents of the University of California.
  36.  * All rights reserved.
  37.  *
  38.  * Redistribution and use in source and binary forms are permitted
  39.  * provided that the above copyright notice and this paragraph are
  40.  * duplicated in all such forms and that any documentation,
  41.  * advertising materials, and other materials related to such
  42.  * distribution and use acknowledge that the software was developed
  43.  * by the University of California, Berkeley.  The name of the
  44.  * University may not be used to endorse or promote products derived
  45.  * from this software without specific prior written permission.
  46.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  47.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  48.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  49.  */
  50.  
  51. #ifndef lint
  52. static char sccsid[] = "@@(#)ruserpass.c    5.1 (Berkeley) 3/1/89";
  53. #endif /* not lint */
  54.  
  55. #include <sys/types.h>
  56. #include <stdio.h>
  57. #include <utmp.h>
  58. #include <ctype.h>
  59. #include <sys/stat.h>
  60. #include <errno.h>
  61. #include "ftp_var.h"
  62.  
  63. char    *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
  64. char    *strcpy();
  65. struct    utmp *getutmp();
  66. static    FILE *cfile;
  67.  
  68. #define    DEFAULT    1
  69. #define    LOGIN    2
  70. #define    PASSWD    3
  71. #define    ACCOUNT 4
  72. #define MACDEF  5
  73. #define    ID    10
  74. #define    MACH    11
  75.  
  76. static char tokval[100];
  77.  
  78. static struct toktab {
  79.     char *tokstr;
  80.     int tval;
  81. } toktab[]= {
  82.     "default",    DEFAULT,
  83.     "login",    LOGIN,
  84.     "password",    PASSWD,
  85.     "passwd",    PASSWD,
  86.     "account",    ACCOUNT,
  87.     "machine",    MACH,
  88.     "macdef",    MACDEF,
  89.     0,        0
  90. };
  91.  
  92. ruserpass(host, aname, apass, aacct)
  93.     char *host, **aname, **apass, **aacct;
  94. {
  95.     char *hdir, buf[BUFSIZ], *tmp;
  96.     char myname[MAXHOSTNAMELEN], *mydomain;
  97.     int t, i, c, usedefault = 0;
  98.     struct stat stb;
  99.     extern int errno;
  100.  
  101.     hdir = getenv("HOME");
  102.     if (hdir == NULL)
  103.         hdir = ".";
  104.     (void) sprintf(buf, "%s/.netrc", hdir);
  105.     cfile = fopen(buf, "r");
  106.     if (cfile == NULL) {
  107.         if (errno != ENOENT)
  108.             perror(buf);
  109.         return(0);
  110.     }
  111.     if (gethostname(myname, sizeof(myname)) < 0)
  112.         myname[0] = '\0';
  113.     if ((mydomain = index(myname, '.')) == NULL)
  114.         mydomain = "";
  115. next:
  116.     while ((t = token())) switch(t) {
  117.  
  118.     case DEFAULT:
  119.         usedefault = 1;
  120.         /* FALL THROUGH */
  121.  
  122.     case MACH:
  123.         if (!usedefault) {
  124.             if (token() != ID)
  125.                 continue;
  126.             /*
  127.              * Allow match either for user's input host name
  128.              * or official hostname.  Also allow match of 
  129.              * incompletely-specified host in local domain.
  130.              */
  131.             if (strcasecmp(host, tokval) == 0)
  132.                 goto match;
  133.             if (strcasecmp(hostname, tokval) == 0)
  134.                 goto match;
  135.             if ((tmp = index(hostname, '.')) != NULL &&
  136.                 strcasecmp(tmp, mydomain) == 0 &&
  137.                 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
  138.                 tokval[tmp - hostname] == '\0')
  139.                 goto match;
  140.             if ((tmp = index(host, '.')) != NULL &&
  141.                 strcasecmp(tmp, mydomain) == 0 &&
  142.                 strncasecmp(host, tokval, tmp - host) == 0 &&
  143.                 tokval[tmp - host] == '\0')
  144.                 goto match;
  145.             continue;
  146.         }
  147.     match:
  148.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  149.  
  150.         case LOGIN:
  151.             if (token())
  152.                 if (*aname == 0) { 
  153.                     *aname = malloc((unsigned) strlen(tokval) + 1);
  154.                     (void) strcpy(*aname, tokval);
  155.                 } else {
  156.                     if (strcmp(*aname, tokval))
  157.                         goto next;
  158.                 }
  159.             break;
  160.         case PASSWD:
  161.             if (strcmp(*aname, "anonymous") &&
  162.                 fstat(fileno(cfile), &stb) >= 0 &&
  163.                 (stb.st_mode & 077) != 0) {
  164.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  165.     fprintf(stderr, "Remove password or correct mode.\n");
  166.                 goto bad;
  167.             }
  168.             if (token() && *apass == 0) {
  169.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  170.                 (void) strcpy(*apass, tokval);
  171.             }
  172.             break;
  173.         case ACCOUNT:
  174.             if (fstat(fileno(cfile), &stb) >= 0
  175.                 && (stb.st_mode & 077) != 0) {
  176.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  177.     fprintf(stderr, "Remove account or correct mode.\n");
  178.                 goto bad;
  179.             }
  180.             if (token() && *aacct == 0) {
  181.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  182.                 (void) strcpy(*aacct, tokval);
  183.             }
  184.             break;
  185.         case MACDEF:
  186.             if (proxy) {
  187.                 (void) fclose(cfile);
  188.                 return(0);
  189.             }
  190.             while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
  191.             if (c == EOF || c == '\n') {
  192.                 printf("Missing macdef name argument.\n");
  193.                 goto bad;
  194.             }
  195.             if (macnum == 16) {
  196.                 printf("Limit of 16 macros have already been defined\n");
  197.                 goto bad;
  198.             }
  199.             tmp = macros[macnum].mac_name;
  200.             *tmp++ = c;
  201.             for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
  202.                 !isspace(c); ++i) {
  203.                 *tmp++ = c;
  204.             }
  205.             if (c == EOF) {
  206.                 printf("Macro definition missing null line terminator.\n");
  207.                 goto bad;
  208.             }
  209.             *tmp = '\0';
  210.             if (c != '\n') {
  211.                 while ((c=getc(cfile)) != EOF && c != '\n');
  212.             }
  213.             if (c == EOF) {
  214.                 printf("Macro definition missing null line terminator.\n");
  215.                 goto bad;
  216.             }
  217.             if (macnum == 0) {
  218.                 macros[macnum].mac_start = macbuf;
  219.             }
  220.             else {
  221.                 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
  222.             }
  223.             tmp = macros[macnum].mac_start;
  224.             while (tmp != macbuf + 4096) {
  225.                 if ((c=getc(cfile)) == EOF) {
  226.                 printf("Macro definition missing null line terminator.\n");
  227.                     goto bad;
  228.                 }
  229.                 *tmp = c;
  230.                 if (*tmp == '\n') {
  231.                     if (*(tmp-1) == '\0') {
  232.                        macros[macnum++].mac_end = tmp - 1;
  233.                        break;
  234.                     }
  235.                     *tmp = '\0';
  236.                 }
  237.                 tmp++;
  238.             }
  239.             if (tmp == macbuf + 4096) {
  240.                 printf("4K macro buffer exceeded\n");
  241.                 goto bad;
  242.             }
  243.             break;
  244.         default:
  245.     fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  246.             break;
  247.         }
  248.         goto done;
  249.     }
  250. done:
  251.     (void) fclose(cfile);
  252.     return(0);
  253. bad:
  254.     (void) fclose(cfile);
  255.     return(-1);
  256. }
  257.  
  258. static
  259. token()
  260. {
  261.     char *cp;
  262.     int c;
  263.     struct toktab *t;
  264.  
  265.     if (feof(cfile))
  266.         return (0);
  267.     while ((c = getc(cfile)) != EOF &&
  268.         (c == '\n' || c == '\t' || c == ' ' || c == ','))
  269.         continue;
  270.     if (c == EOF)
  271.         return (0);
  272.     cp = tokval;
  273.     if (c == '"') {
  274.         while ((c = getc(cfile)) != EOF && c != '"') {
  275.             if (c == '\\')
  276.                 c = getc(cfile);
  277.             *cp++ = c;
  278.         }
  279.     } else {
  280.         *cp++ = c;
  281.         while ((c = getc(cfile)) != EOF
  282.             && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  283.             if (c == '\\')
  284.                 c = getc(cfile);
  285.             *cp++ = c;
  286.         }
  287.     }
  288.     *cp = 0;
  289.     if (tokval[0] == 0)
  290.         return (0);
  291.     for (t = toktab; t->tokstr; t++)
  292.         if (!strcmp(t->tokstr, tokval))
  293.             return (t->tval);
  294.     return (ID);
  295. }
  296. @
  297.  
  298.  
  299. 1.2
  300. log
  301. @Declare forward references to keep gcc happy.
  302. @
  303. text
  304. @d6 10
  305. a15 5
  306.  * provided that this notice is preserved and that due credit is given
  307.  * to the University of California at Berkeley. The name of the University
  308.  * may not be used to endorse or promote products derived from this
  309.  * software without specific prior written permission. This software
  310.  * is provided ``as is'' without express or implied warranty.
  311. d19 1
  312. a19 1
  313. static char sccsid[] = "@@(#)ruserpass.c    1.4 (Berkeley) 3/14/88";
  314. d22 1
  315. a22 10
  316. struct macel {
  317.     char mac_name[9];    /* macro name */
  318.     char *mac_start;    /* start of macro in macbuf */
  319.     char *mac_end;        /* end of macro in macbuf */
  320. };
  321.  
  322. extern int macnum, proxy;            /* number of defined macros */
  323. extern struct macel macros[16], *macpt;
  324. extern char macbuf[4096];
  325.  
  326. a25 1
  327. #include <sys/types.h>
  328. d28 1
  329. d30 2
  330. a31 3
  331. extern int    rnetrc(), token();
  332. extern char    *renvlook(), *malloc(), *index(), *getlogin();
  333. extern char    *getenv(), *getpass(), *strcpy();
  334. a34 9
  335. ruserpass(host, aname, apass, aacct)
  336.     char *host, **aname, **apass, **aacct;
  337. {
  338.  
  339.     /* renv(host, aname, apass, aacct);
  340.     if (*aname == 0 || *apass == 0) */
  341.         return(rnetrc(host, aname, apass, aacct));
  342. }
  343.  
  344. d41 1
  345. a41 1
  346. #define    MACHINE    11
  347. d52 1
  348. d54 1
  349. a54 1
  350.     "machine",    MACHINE,
  351. d59 1
  352. a59 2
  353. static
  354. rnetrc(host, aname, apass, aacct)
  355. d63 2
  356. a64 1
  357.     int t, i, c;
  358. d78 4
  359. d86 2
  360. a87 2
  361.         (void) token();
  362.         continue;
  363. d89 23
  364. a111 2
  365.     case MACHINE:
  366.         if (token() != ID || strcmp(host, tokval))
  367. d113 3
  368. a115 1
  369.         while ((t = token()) && t != MACHINE) switch(t) {
  370. d128 3
  371. a130 2
  372.             if (fstat(fileno(cfile), &stb) >= 0
  373.                 && (stb.st_mode & 077) != 0) {
  374. d133 1
  375. a133 1
  376.                 return(-1);
  377. d145 1
  378. a145 1
  379.                 return(-1);
  380. d154 1
  381. d160 1
  382. a160 1
  383.                 return(-1);
  384. d164 1
  385. a164 1
  386.                 return(-1);
  387. d174 1
  388. a174 1
  389.                 return(-1);
  390. d182 1
  391. a182 1
  392.                 return(-1);
  393. d194 1
  394. a194 1
  395.                     return(-1);
  396. d208 1
  397. a208 1
  398.                 return(-1);
  399. d220 3
  400. @
  401.  
  402.  
  403. 1.1
  404. log
  405. @Initial revision
  406. @
  407. text
  408. @d34 3
  409. a36 2
  410. char    *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
  411. char    *strcpy();
  412. @
  413.